<?php

/**
 * ECSHOP 礼品商城列表
 * $Author: liubo $
 * $Id: category.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */

/* 获得请求的分类 ID */
if (isset($_REQUEST['id']))
{
    $cat_id = intval($_REQUEST['id']);
}
elseif (isset($_REQUEST['cat_id']))
{
    $cat_id = intval($_REQUEST['cat_id']);
}
else
{
	$cat_id = 0;//默认分类ID为0
}


/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 12;
$price_min = isset($_REQUEST['price_min']) && intval($_REQUEST['price_min'])  > 0 ? intval($_REQUEST['price_min'])  : 0;
$price_max = isset($_REQUEST['price_max']) && intval($_REQUEST['price_max'])  > 0 ? intval($_REQUEST['price_max'])  : 0;
$cat2_id = isset($_REQUEST['cat2_id']) && intval($_REQUEST['cat2_id'])  > 0 ? intval($_REQUEST['cat2_id'])  : 0;

/* 排序、显示方式以及类型 */
$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
$default_sort_order_type   = 'goods_id';

$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('click_count', 'shop_price', 'last_update'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))? trim($_REQUEST['order']) : $default_sort_order_method;

/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */

/* 页面的缓存ID */
$cache_id = sprintf('%X', crc32($cat_id .'-'.$cat2_id.'-'.$sort  .'-' . $order  .'-' . $page . '-' . $size . '-' . $price_min . '-' . $price_max . '-' . 'gift_cat' ));

if (!$smarty->is_cached('gift_cat.dwt', $cache_id))
{

	$category_title = '礼品商城商品';

	assign_template('gc', array($cat_id));

    $smarty->assign('categories',       get_gift_cat_list(0,$cat_id,false,1,false)); // 礼品分类
	if($cat_id>0){
		$smarty->assign('categories2',      get_gift_cat_list($cat_id,$cat2_id,false,0,false)); // 礼品二级分类
	}else{
		$smarty->assign('categories2',      gift_cat_list($cat2_id,false)); // 礼品二级分类
	}
    $smarty->assign('top_goods',        get_gift_top10());             // 销售排行
    $smarty->assign('cat_id',			$cat_id);  //礼品分类ID
	$smarty->assign('cat2_id',			$cat2_id);  //礼品分类二级ID
	$smarty->assign('price_min',		$price_min);  //最高价格
	$smarty->assign('price_max',		$price_max);  //最低价格
	$smarty->assign('category_title',	$category_title);  //礼品分类ID

	$cat = ($cat2_id>0)?$cat2_id:$cat_id;

    $count = get_gift_goods_count($cat,$price_min,$price_max);
    $max_page = ($count> 0) ? ceil($count / $size) : 1;
    if ($page > $max_page)
    {
        $page = $max_page;
    }
    $gift_goods_list = get_gift_goods_list($cat,$price_min,$price_max, $size, $page, $sort, $order);
    $smarty->assign('gift_goods_list',       $gift_goods_list);

	$keyword = '';
	assign_pager('exchange', $cat_id, $count, $size, $sort, $order, $page,$keywords, $brand, $price_min, $price_max);
    assign_dynamic('exchange'); // 动态内容

	//print_r($goods_img_list);exit;
}
$smarty->assign('menu_index',		'10');//当前导航标识
$smarty->display('gift_cat.dwt', $cache_id);


/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info2($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_name FROM ' . $GLOBALS['ecs']->table('gift_cat') . " WHERE cat_id = '$cat_id'");
}

/**
 * 获得分类下的稿件
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function get_gift_goods_list($cat_id,$price_min,$price_max, $size, $page, $sort, $order)
{
	$where = " g.is_on_sale = 1 ";
	
    if ($cat_id > 0)
    {
        $where .= " AND ".get_children3($cat_id);
    }

    if ($price_max > 0)
    {
        $where .= " AND (g.shop_price >=$price_min AND g.shop_price <= $price_max) ";
    }

    /* 获得商品列表 */
    $sql = 'SELECT g.* ' .
            'FROM ' . $GLOBALS['ecs']->table('gift_goods') . ' AS g ' .
            "WHERE $where ORDER BY $sort $order";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['goods_pack']       = $row['goods_pack'];
		$arr[$row['goods_id']]['integral']         = intval($row['integral'])*100;
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'gift_goods.php?id='.$row['goods_id'];
		$arr[$row['goods_id']]['cat_name']		   = get_gift_cat_name($row['cat_id']);
		$arr[$row['goods_id']]['short_name']	   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                               sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
    }

    return $arr;
}

/**
 * 获得分类下的稿件总数
 *
 * @access  public
 * @param   string     $cat_id
 * @return  integer
 */
function get_gift_goods_count($cat_id = 0, $price_min = 0 , $price_max = 0)
{
    $where = " g.is_on_sale = 1 ";
	
    if ($cat_id > 0)
    {
        $where .= " AND  ".get_children3($cat_id);;
    }

    if ($price_max > 0)
    {
        $where .= " AND (g.shop_price >=$price_min AND g.shop_price <= $price_max) ";
    }

    /* 返回商品总数 */
    return $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('gift_goods') . " AS g WHERE $where");
}


/* 获取礼品分类列表*/
function gift_cat_list($selected = 0 ,$re_type = true ){
		$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('gift_cat') ." WHERE parent_id <> 0 AND is_show = '1' GROUP BY sort_order ASC, cat_id DESC";
		$res = $GLOBALS['db']->getAll($sql);
		$list = array();
		if ($re_type == true){
			$select = '';
			foreach ($res AS $var)
			{
				$select .= '<option value="' . $var['cat_id'] . '" ';
				$select .= ($selected == $var['cat_id']) ? "selected='ture'" : '';
				$select .= '>';
				$select .= htmlspecialchars(addslashes($var['cat_name']), ENT_QUOTES) . '</option>';
			}
			return $select;
		}else{
			foreach ($res AS $var)
			{
				$list[] = $var;
			}
			return $list;
		}
	}

function get_gift_cat_name($cat_id){
    return $GLOBALS['db']->getOne('SELECT cat_name FROM ' . $GLOBALS['ecs']->table('gift_cat') .
        " WHERE cat_id = '$cat_id'");
}

/**
 * 调用当前分类的销售排行榜
 *
 * @access  public
 * @param   string  $cats   查询的分类
 * @return  array
 */
function get_gift_top10($cats = '')
{

    $where = !empty($cats) ? "AND g.cat_id = '$cats' " : '';

    /* 排行统计的时间 */
    switch ($GLOBALS['_CFG']['top10_time'])
    {
        case 1: // 一年
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 365 * 86400) . "'";
        break;
        case 2: // 半年
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 180 * 86400) . "'";
        break;
        case 3: // 三个月
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 90 * 86400) . "'";
        break;
        case 4: // 一个月
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 30 * 86400) . "'";
        break;
        default:
            $top10_time = '';
    }

    $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price, g.goods_thumb, SUM(og.goods_number) as goods_number ' .
           'FROM ' . $GLOBALS['ecs']->table('gift_goods') . ' AS g, ' .
                $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
                $GLOBALS['ecs']->table('order_goods') . ' AS og ' .
           "WHERE g.is_on_sale = 1 $where $top10_time " ;

    $sql .= ' AND og.order_id = o.order_id AND og.goods_id = g.goods_id AND og.extension_code = "gift_goods_buy"' .
           "AND (o.order_status = '" . OS_CONFIRMED .  "' OR o.order_status = '" . OS_SPLITED . "') " .
           "AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') " .
           "AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') " .
           'GROUP BY g.goods_id ORDER BY goods_number DESC, g.goods_id DESC LIMIT ' . $GLOBALS['_CFG']['top_number'];
           
    $arr = $GLOBALS['db']->getAll($sql);

    for ($i = 0, $count = count($arr); $i < $count; $i++)
    {
        $arr[$i]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                    sub_str($arr[$i]['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $arr[$i]['goods_name'];
        $arr[$i]['url']     = 'gift_goods.php?id='.$arr[$i]['goods_id'];
        $arr[$i]['thumb']	= get_image_path($arr[$i]['goods_id'], $arr[$i]['goods_thumb'],true);
		$arr[$i]['market_price'] = price_format($arr[$i]['market_price']);
		$arr[$i]['shop_price']   = price_format($arr[$i]['shop_price']);
    }

    return $arr;
}


?>
